﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

namespace GatewayServerService.Data
{
    public class DBIotDeviceLiveStateCache
    {


        /// <summary>
        /// 缓存增加 获取变量列表
        /// </summary>
        /// <param name="cguid"></param>
        /// <returns></returns>
        public iot_device_live_state getLiveStateByCache(string dguid)
        {
            try
            {
                iot_device_live_state model = UtilMemoryCacheHelper.StringGet<iot_device_live_state>("getLiveStateByCache" + dguid);
                if (model == null)
                {
                    EFYihongIotContext ef = new EFYihongIotContext();
                    model = ef.iot_device_live_state.Where(c => c.ls_d_guid == dguid).FirstOrDefault();
                    UtilMemoryCacheHelper.StringSet("getLiveStateByCache" + dguid, model, 1);
                    ef.Dispose();
                }
                return model;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }

        }

        /// <summary>
        /// 缓存增加 获取变量列表
        /// </summary>
        /// <param name="cguid"></param>
        /// <returns></returns>
        public iot_device_live_state getGateWayIsOnlineByCache(string dguid)
        {
            iot_device_live_state model = UtilMemoryCacheHelper.StringGet<iot_device_live_state>("getGateWayIsOnlineByCache1" + dguid);
            if (model == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                model = ef.iot_device_live_state.Where(c => c.ls_d_guid == dguid && c.ls_live_state_set_time > DateTime.Now.AddMinutes(-1)).FirstOrDefault();
                if (model != null)
                {
                    UtilMemoryCacheHelper.StringSet("getGateWayIsOnlineByCache1" + dguid, model, 1);
                }
                ef.Dispose();
            }

            return model;
        }

    }
}
